
package ch.hearc.coursJava.IO.RMI.exemple0.pcB.moo;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class StringCrypte implements Serializable
	{

	/*------------------------------------------------------------------*\
	|*							Constructeurs							*|
	\*------------------------------------------------------------------*/
	public StringCrypte(String secret)
		{
		this.secret = secret;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Public							*|
	\*------------------------------------------------------------------*/

	@Override
	public String toString()
		{
		return secret;
		}

	/*------------------------------*\
	|*				Get				*|
	\*------------------------------*/
	public String getSecret()
		{
		return this.secret;
		}

	/*------------------------------------------------------------------*\
	|*							Methodes Private						*|
	\*------------------------------------------------------------------*/
	/*------------------------------*\
	|*		Serialisation			*|
	\*------------------------------*/

	/**
	 * Serialization (PcA)
	 */
	private void writeObject(ObjectOutputStream out) throws IOException
		{
		//System.out.println("Entree dans le writeObject");
		out.writeObject(crypter(this.secret));
		}

	/**
	 * Deserialization (PcB)
	 */
	private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
		{
		//System.out.println("Entree dans le readObject");
		String secretCrypte = (String)in.readObject();
		this.secret = decrypter(secretCrypte);
		}

	private String crypter(String secret)
		{
		return secret + "todo";
		//TODO Faire qqch de plus interessant. Cf cours de crypto
		}

	private String decrypter(String secret)
		{
		return secret.substring(0, secret.length() - 4);
		}

	/*------------------------------------------------------------------*\
	|*							Attributs Private						*|
	\*------------------------------------------------------------------*/
	private String secret;

	}
